package com.egg.ylt.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.adapter.ADA_BabyManageRecycler;
import com.egg.ylt.pojo.MemberBabyEntity;
import com.egg.ylt.presenter.impl.BabyManagePresenterImpl;
import com.egg.ylt.view.IBabyManageView;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.AppSharedPreferences;
import com.yonyou.framework.library.common.utils.ListUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class ACT_BabyManage extends BaseActivity<BabyManagePresenterImpl> implements IBabyManageView {

    @BindView(R.id.include_iv_back)
    ImageView includeIvBack;
    @BindView(R.id.include_tv_title)
    TextView includeTvTitle;
    @BindView(R.id.include_tv_right)
    TextView includeTvRight;
    @BindView(R.id.include_rl_view)
    RelativeLayout includeRlView;
    @BindView(R.id.recycler_baby)
    RecyclerView recyclerBaby;
    @BindView(R.id.target_view_ll)
    LinearLayout llTargetView;

    private ADA_BabyManageRecycler recyclerAdapter;
    private AppSharedPreferences mSp;
    private String babyId;

    @Override
    protected void getBundleExtras(Bundle extras) {
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_baby_manage;
    }

    @Override
    protected View getLoadingTargetView() {
        return llTargetView;
    }

    @Override
    protected void initViewsAndEvents() {
        mSp = new AppSharedPreferences(mContext);
        //初始化view
        initView();
        //设置recycle
        initRecycler();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mPresenter != null){
            mPresenter.getBabyData(Constants.TOKEN);
        }
    }

    private void initRecycler() {
        //设置布局管理器
        recyclerBaby.setLayoutManager(new LinearLayoutManager(mContext));
        //设置适配器
        recyclerAdapter = new ADA_BabyManageRecycler(mContext);
        recyclerBaby.setAdapter(recyclerAdapter);
        //条目监听
        recyclerAdapter.setOnItemClickListener(new ADA_BabyManageRecycler.OnItemClickListener() {
            @Override
            public void onClick(MemberBabyEntity.ListBean bean) {
                Bundle mBundle = new Bundle();
                mBundle.putSerializable("bean", bean);
                readyGo(ACT_EditBabyInfo.class, mBundle);
            }
        });

        //删除按钮
        recyclerAdapter.setOnDeleteClickListener(new ADA_BabyManageRecycler.OnDeleteClickListener() {
            @Override
            public void onDeleteClick(String id) {
                mPresenter.deleteBaby(Constants.TOKEN, id);
            }
        });

        //设置默认宝宝
        recyclerAdapter.setOnDefaultClickListener(new ADA_BabyManageRecycler.OnDefaultClickListener() {
            @Override
            public void onDefaultClick(String lastId, String currentId) {
                babyId = currentId;
                mPresenter.setBabyDefault(Constants.TOKEN, lastId, currentId);
            }
        });

    }

    private void initView() {
        //设置title
        includeTvTitle.setText("宝宝管理");
        //title右边文字
        includeTvRight.setText("添加");
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @OnClick({R.id.include_iv_back, R.id.include_tv_right})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.include_iv_back://返回键
                finish();
                break;
            case R.id.include_tv_right://添加宝宝
                readyGo(ACT_SelectBaby.class);
                break;
        }
    }

    @Override
    public void getBabyData(MemberBabyEntity entity) {
        if (ListUtil.isListEmpty(entity.getList())){
            toggleShowEmpty(true, R.drawable.empty_no_response, "暂无宝宝信息");
            return;
        }

        toggleShowEmpty(false, -1, "");

        // 设置数据
        recyclerAdapter.addList(entity.getList());
    }

    @Override
    public void savaBabySucceed() {
        //把默认宝宝的id存入本地
//        mSp.putString(Constants.BABY_ID, babyId);
        mPresenter.getBabyData(Constants.TOKEN);
    }
}
